function replace_all(Source,stringToFind,stringToReplace){
  var temp = Source;
    var index = temp.indexOf(stringToFind);
        while(index != -1){
            temp = temp.replace(stringToFind,stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
}
    
    function upload(BtnUpload, SubmitVar, contentId ,Status, image_url, base_path){
        var time = (new Date).getTime();
        new AjaxUpload((BtnUpload), {
            action: '?r=upload',
            name: SubmitVar,
            data: {
                r: 'upload',
                timestamp: time,
            },
            responseType: false,
            onSubmit: function(file, ext){
                 if (! (ext && /^(jpg|png|jpeg|bmp)$/.test(ext))){ 
                    alert("Chi cho phép file jpg, png");
                }
                Status.text('Đang tải ... ');
            },
            onComplete: function(file, response){
                Status.text('');
                if(response == 'success'){
                    var temp_src = base_path+time+'-'+file;
                    var src = replace_all(temp_src,' ','');
                    contentId.attr('src',src);
                    image_url.val(src);
                    //contentId.append('<img id="'+SubmitVar+'_img" src="'+src+'" style="width:200px;height:200px" ></img>');
                }else{
                    alert('Upload fail');
                }
            }
        });
    }
    
    function uploadWithAppend(BtnUpload, SubmitVar, contentId, contentUrlId, base_path){
        var time = (new Date).getTime();
        new AjaxUpload((BtnUpload), {
            action: '?r=upload',
            name: SubmitVar,
            data: {
                r: 'upload',
                timestamp: time,
            },
            responseType: false,
            onSubmit: function(file, ext){
                 if (! (ext && /^(jpg|png|jpeg|bmp)$/.test(ext))){ 
                    alert("Chi cho phép file jpg, png");
                }
            },
            onComplete: function(file, response){
                if(response == 'success'){
                    file = time+'-'+file
                    file = replace_all(file,' ','');
                    var src = base_path+file;
                    var url_string = contentUrlId.val();
                     url_string = url_string + src+';';
                     contentUrlId.val(url_string);
                     //alert(url_string);
                     image = '<div id="'+src+'" style="float:left;margin:10px 10px 0px 0px;width:100px;">';
                     image +='<img src="'+src+'" class="screenshot_image" style="width:100px;height:100px;" ></img>';
                     image +='<a href="javascript:void(0)" title="'+src+'" onclick="removeScreenshot(this.title);">Remove</a>';
                     image +='</div>';
                     contentId.append(image);
                }else{
                    alert('Upload fail');
                }
            }
        });
    }
    
    function removeScreenshot(file_name){
        dataString = 'file_name='+file_name;
        $.ajax({
             type: 'post',
             url: '?r=game/removeScreenshot',
             data: dataString,
             dataType: "html",
             success: function(data){
                 if(data == 'success'){
                     var url_string = $('#screenshots_url').val();
                     url_string = replace_all(url_string,file_name+';','');
                     $('#screenshots_url').val(url_string);
                     element = document.getElementById(file_name);
                     element.parentNode.removeChild(element);
                 }
             }
         });
    }

$(document).ready(function(){
    upload( $("#thumb"), 'thumb', $("#thumb_content"), $('#thumb_status'), $('#thumb_url'),'http://localhost/vqm/admin.gomo.vn/uploads/images/thumbs/' );
    upload( $("#icon"), 'icon', $("#icon_content"), $('#icon_status'),$('#icon_url'),'http://localhost/vqm/admin.gomo.vn/uploads/images/icons/');
    uploadWithAppend( $("#screenshot"), 'screenshot', $("#screenshots_content"), $("#screenshots_url"),'http://localhost/vqm/admin.gomo.vn/uploads/images/screenshots/');

});